<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  <title>NodeJS 库 (持续更新) | Storm</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="1.加密 解密 工具 bcrypt     加密   1234567891011121314const saltRounds = 10; //加密轮数let password = userInfo.password //密码bcrypt.genSalt(saltRounds, function (err, salt) &amp;#123;        bcrypt.hash(password, salt">
<meta name="keywords" content="NodeJS">
<meta property="og:type" content="article">
<meta property="og:title" content="NodeJS 库 (持续更新)">
<meta property="og:url" content="https://storm4542.github.io/archives/1dd248d5.html">
<meta property="og:site_name" content="Storm">
<meta property="og:description" content="1.加密 解密 工具 bcrypt     加密   1234567891011121314const saltRounds = 10; //加密轮数let password = userInfo.password //密码bcrypt.genSalt(saltRounds, function (err, salt) &amp;#123;        bcrypt.hash(password, salt">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2018-09-30T08:24:26.257Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="NodeJS 库 (持续更新)">
<meta name="twitter:description" content="1.加密 解密 工具 bcrypt     加密   1234567891011121314const saltRounds = 10; //加密轮数let password = userInfo.password //密码bcrypt.genSalt(saltRounds, function (err, salt) &amp;#123;        bcrypt.hash(password, salt">
  
    <link rel="alternative" href="https://storm4542.github.io/atom.xml" title="Storm" type="application/atom+xml">
  
  
    <link rel="icon" href="https://storm4542.github.io/favicon.png">
  
  <link href="static/css/css.css" rel="stylesheet" type="text/css">
  <link rel="stylesheet" href="static/css/style.css">
  

</head></html>
<body>
  <div id="container">
    <div id="wrap">
      <div class="outer">
        <section id="main"><article id="post-NodeJS-库-持续更新" class="article article-type-post" itemscope="" itemprop="blogPost">
  
  <div class="article-inner">
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      NodeJS 库 (持续更新)
    </h1>
  

      </header>
    
    <div class="article-meta">
      <a href="" class="article-date">
  <time datetime="2018-09-30T08:22:41.000Z" itemprop="datePublished">2018-09-30</time>
</a>
      
    </div>
    <div class="article-entry" itemprop="articleBody">
      
        <h3 id="1-加密-解密-工具"><a href="#1-加密-解密-工具" class="headerlink" title="1.加密 解密 工具"></a>1.加密 解密 工具</h3><ul>
<li>bcrypt </li>
</ul>
<p>  加密</p>
  <figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> saltRounds = <span class="number">10</span>; <span class="comment">//加密轮数</span></span><br><span class="line"><span class="keyword">let</span> password = userInfo.password <span class="comment">//密码</span></span><br><span class="line">bcrypt.genSalt(saltRounds, <span class="function"><span class="keyword">function</span> (<span class="params">err, salt</span>) </span>&#123;</span><br><span class="line">        bcrypt.hash(password, salt, <span class="function"><span class="keyword">function</span> (<span class="params">err, hash</span>) </span>&#123;</span><br><span class="line">            <span class="comment">//把hash值赋值给password变量</span></span><br><span class="line">            password = hash;</span><br><span class="line">            storeUInfo(&#123; <span class="comment">//存储</span></span><br><span class="line">                username: userInfo.username,</span><br><span class="line">                password,</span><br><span class="line">                name: userInfo.name,</span><br><span class="line">                age: userInfo.age</span><br><span class="line">            &#125;);</span><br><span class="line">        &#125;)</span><br><span class="line">    &#125;)</span><br></pre></td></tr></table></figure>
<p>  解密验证</p>
  <figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">async</span> <span class="function"><span class="keyword">function</span> <span class="title">login</span>(<span class="params">userInfo</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">const</span> user = <span class="keyword">await</span> UserModel.findOne(&#123; <span class="comment">//从数据库中找到该用户</span></span><br><span class="line">        username: userInfo.username</span><br><span class="line">    &#125;)</span><br><span class="line">    <span class="keyword">const</span> match = <span class="keyword">await</span> bcrypt.compare(userInfo.password, user.password) <span class="comment">// 看看 密码是否相同</span></span><br><span class="line">    <span class="keyword">if</span> (match) &#123;</span><br><span class="line">        <span class="keyword">return</span> user</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="keyword">throw</span> <span class="keyword">new</span> HttpParamError(<span class="string">'user'</span>, <span class="string">'用户名密码错误'</span>, <span class="string">'username password error'</span>)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

      

      
        
    </div>
  </div>
  
    
<nav id="article-nav">
  
    <a href="273360f2.html" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          mongoose 使用
        
      </div>
    </a>
  
  
    <a href="a431a66.html" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">Vue父子传参的方式</div>
    </a>
  
</nav>

  
</article>


<section id="comments">
  <link rel="stylesheet" href="static/css/default.css">
  <script src="static/js/gitment.browser.js"></script>
  <script>
    var gitment = new Gitment({
      owner: 'your github account',
      repo: 'your repo',
      oauth: {
        client_id: 'your client_id',
        client_secret: 'your client_secret',
      },
    })
    gitment.render('comments')
  </script>
</section>

</section>
        <aside id="sidebar">
  <nav class="menus">
  	<ul>
  		<li><a href="index.html"><i class="icon icon-home"></i></a></li>
  		
			<li><a href="index1.html"><i class="icon icon-fenlei"></i></a></li>
  		
  		
			<li><a href="index2.html"><i class="icon icon-tag"></i></a></li>
  		
  		
  			<li><a href="javascript:;" target="_blank" rel="external nofollow noopener noreferrer"><i class="icon icon-github"></i></a></li>
  		
  	</ul>
  </nav>
  <a id="go-top" href="#"><i class="icon icon-up"></i></a>
</aside>

      </div>
      <footer id="footer">
  
	<div id="footer-info" class="inner">
	  &copy; 2019 Storm 
	  - Powered by <a href="javascript:;" target="_blank" rel="external nofollow noopener noreferrer">Hexo</a>
	  - Theme <a href="javascript:;" target="_blank" rel="external nofollow noopener noreferrer">Jane</a>
	</div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="index.html" class="mobile-nav-link">Home</a>
  
    <a href="index1.html" class="mobile-nav-link">Archives</a>
  
    <a href="index2.html" class="mobile-nav-link">Tag</a>
  
    <a href="javascript:;" class="mobile-nav-link" rel="external nofollow noopener noreferrer" target="_blank">Github</a>
  
</nav>
    
<script>
  var disqus_shortname = 'storm';
  
  var disqus_url = 'https://storm4542.github.io/archives/1dd248d5.html';
  
  (function(){
    var dsq = document.createElement('script');
    dsq.type = 'text/javascript';
    dsq.async = true;
    dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
    (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
  })();
</script>


<script src="static/js/jquery.min.js"></script>


  <link rel="stylesheet" href="static/css/jquery.fancybox.css">
  <script src="static/js/jquery.fancybox.pack.js"></script>


<script src="static/js/script.js"></script>

  </div>
</body>
